//
//  Panel.m
//  InstrumentPanel
//
//  Created by nav on 16/6/24.
//  Copyright © 2016年 Chris.C. All rights reserved.
//

#import "Panel.h"
#import "Indicator.h"
@implementation Panel
{
    CGFloat markInset;
    CGFloat markWidth;
    CGFloat angleRation;
    CGFloat range1;
    CGFloat range2;
    CGFloat radiusOfOval;
    CGFloat radius;
    NSArray *titleArray1;
    NSArray *titleArray2;
    Indicator *indicatorView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        markInset = 20;
        markWidth = 12;
        angleRation = M_PI_2/30.0;
        titleArray1 = @[@"0",@"20"];
        titleArray2 = @[@"40",@"60",@"80",@"100",@"120",@"140",@"160"];
        
        radius = CGRectGetWidth(frame)/2;
        radiusOfOval = CGRectGetWidth(frame)/2 - markInset;
        if(indicatorView == nil) {
            indicatorView = [[Indicator alloc]initWithFrame:CGRectMake(0, 0, 39, 90)];
            CGPoint anchorPoint = indicatorView.layerAnchorPoint;
            indicatorView.layer.anchorPoint = anchorPoint;
            indicatorView.layer.position = CGPointMake(CGRectGetWidth(frame)/2, CGRectGetHeight(frame)/2);
            indicatorView.transform = CGAffineTransformMakeRotation(-(M_PI_2)*4/3);
            [self addSubview:indicatorView];
            
        }
        
        
        for (int i = 0; i <= 70; i++) {
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 1)];
            if(i%10 == 0) {
                view.frame = CGRectMake(0, 0, 15, 1);
                int arrayindex = (i/10);
                if(arrayindex !=0) {
                    
                    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectZero];
                    lab.font = [UIFont systemFontOfSize:12];
                    lab.textColor = [UIColor whiteColor];
                    lab.text = titleArray2[arrayindex-1];
                    CGFloat labelCenterX = CGRectGetWidth(frame)/2 -radius * cos((i)*angleRation);
                    CGFloat labelCenterY = CGRectGetHeight(frame)/2 - radius *sin(i * angleRation);
                    [lab sizeToFit];
                    lab.center = CGPointMake(labelCenterX, labelCenterY);
                    [self addSubview:lab];
                    
                }
                
            }
            CGFloat centerX = CGRectGetWidth(frame)/2 -radiusOfOval * cos((i)*angleRation);
            CGFloat centerY = CGRectGetHeight(frame)/2 - radiusOfOval *sin(i * angleRation);
            view.backgroundColor = [UIColor greenColor];
            view.transform = CGAffineTransformMakeRotation(i * angleRation);
            view.center = CGPointMake(centerX, centerY);
            [self addSubview:view];
            
        }
        
        
        for (int j = 110; j <=120; j++) {
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 1)];
            if(j%10 == 0) {
                view.frame = CGRectMake(0, 0, 15, 1);
                int arrayindex = j/10%10 - 1;
                
                UILabel *lab = [[UILabel alloc]initWithFrame:CGRectZero];
                lab.font = [UIFont systemFontOfSize:12];
                lab.textColor = [UIColor whiteColor];
                lab.text = titleArray1[arrayindex];
                CGFloat labelCenterX = CGRectGetWidth(frame)/2 -radius * cos((j)*angleRation);
                CGFloat labelCenterY = CGRectGetHeight(frame)/2 - radius *sin(j * angleRation);
                [lab sizeToFit];
                lab.center = CGPointMake(labelCenterX, labelCenterY);
                [self addSubview:lab];
                
            }
            CGFloat centerX = CGRectGetWidth(frame)/2 -radiusOfOval * cos((j)*angleRation);
            CGFloat centerY = CGRectGetHeight(frame)/2 - radiusOfOval *sin(j * angleRation);
            view.backgroundColor = [UIColor greenColor];
            view.transform = CGAffineTransformMakeRotation(j * angleRation);
            view.center = CGPointMake(centerX, centerY);
            [self addSubview:view];
            
        }
        
        
    }
    return self;
}
- (void)setIndicatorTransform:(CGFloat)angle
{
    indicatorView.transform = CGAffineTransformMakeRotation(angle);
}

@end
